/**
 * Created by Administrator on 2018/5/27 0027.
 */
//easyui  验证框重写


/*上传等待*/
function loading() {
    $("<div class=\"datagrid-mask\" style='z-index: 99999;'></div>").css({
        display: "block",
        width: "100%",
        height: $(window).height()
    }).appendTo("body");
    $("<div class=\"datagrid-mask-msg\"></div>").html("数据导入，请稍候。。。").appendTo("body").css({
        display: "block",
        left: ($(document.body).outerWidth(true) - 190) / 2,
        top: ($(window).height() - 45) / 2
    });
}
// 取消加载层
function disLoading() {
    $(".datagrid-mask").remove();
    $(".datagrid-mask-msg").remove();
}


function downloadMuban() {
    window.location.href = path + "/production/downloadExcelMuban.xls";
}
//导出Excel
function downExcel() {
    window.location.href = path + "/production/downloadproduct.xls?product=" + $("#ss").searchbox("getValue") + "&state=" + $("#ss").searchbox("getName");
}

//导入excel
function daoruExcel() {
    var fileName = $("#excel")[0].files[0].name;
    var pattern = /(\.*.xls$)|(\.*.xlsx$)/;
    if (!pattern.test(fileName)) {
        $.messager.alert('警告', '系统仅支持.xls和.xlsx的表格!');
        return;
    }
    var formData = new FormData();
    var gongyingshang = $("#gongyingshang").combobox("getValue");
    formData.append('excel', $("#excel")[0].files[0]);
    formData.append('gongyingshang', gongyingshang);
    $("#daoExcel").window('close');
    $.ajax({
        type: 'POST',
        url: path + "/production/daoruexcel.do",
        processData: false,
        contentType: false,
        data: formData,
        dataType: "json",
        beforeSend: function () {
            loading();
            $("#daoru").linkbutton('disable');
        },
        success: function (data) {
            disLoading();
            $("#daoru").linkbutton('enable');
            if (data.code == 200) {
                $.messager.show({
                    title: '提示',
                    msg: '导入成功！',
                    timeout: 3000,
                    showType: 'slide'
                });
                $("#dg").datagrid({
                    url: path + '/production/findall.do'
                });
                $("#excel").val("");
            } else {
                $.messager.alert('警告', data.msg);
            }
        },
        complete: function (XMLHttpRequest, status) { // 请求完成后最终执行参数
            if (status == 'timeout') {// 超时,status还有success,error等值的情况
                ajaxTimeoutTest.abort();
                $.messager.show({
                    title: '提示',
                    msg: '请求超时！',
                    timeout: 3000,
                    showType: 'slide'
                });
            }
            disLoading();
            $("#daoru").linkbutton('enable');
        },
        error: function () {
            disLoading();
            $.messager.alert('警告', '导入失败，未知错误！');
            $("#daoru").linkbutton('enable');
        }
    })
}
//删除chanpin
function shanchu() {
    var row = $('#dg').datagrid('getSelected');
    if (row) {
        $.messager.confirm('Confirm', '是否确认删除' + row.product + '?', function (r) {
            if (r) {
                $.ajax({
                    url: path + '/production/del.do',
                    dataType: 'json',
                    type: 'POST',
                    data: {
                        id: row.id
                    },
                    success: function (data) {
                        if (data.code == 200) {
                            $.messager.show({
                                title: '提示',
                                msg: "删除" + row.product + '成功!',
                                timeout: 3000,
                                showType: 'slide'
                            });
                            $("#dg").datagrid({
                                url: path + '/production/findall.do'
                            });
                        } else {
                            $.messager.alert('提示', data.msg);
                        }
                    }
                })
            }
        });
    } else {
        $.messager.alert('提示', '请选择要操作的数据');
    }


}
//打开窗口
function openWindow(code) {
    clear();
    if (code == 2) {
        $('#addwindow').dialog({title: "修改款号"});
        var row = $('#dg').datagrid('getSelected');
        if (row) {
            if (row.state != 0) {
                $.messager.alert('提示', '该状态下不可修改信息');
                return;
            }
            loadCombobox();
            $.ajax({
                url: path + '/production/find/id.do',
                dataType: 'json',
                type: 'POST',
                data: {
                    id: row.id
                },
                success: function (data) {
                    $("#id").val(data.data.id)
                    $("#kuanhao").textbox("setValue", data.data.product);
                    $("#kuanming").textbox("setValue", data.data.productName);
                    $("#mingpaijia").numberbox("setValue", data.data.price);
                    $("#kuanshi").combobox("setValue", data.data.kuanshi);
                    $("#pinpai").combobox("setValue", data.data.pinpai);
                    $("#kuanxing").combobox("setValue", data.data.style);
                    $("#dalei").combobox("setValue", data.data.dalei);
                    $("#xiaolei").combobox("setValue", data.data.category);
                    $("#gongyings").combobox("setValue", data.data.gongyingshang);
                    $("#xingbie").combobox("setValue", data.data.sex);
                    $("#year").combobox("setValue", data.data.year);
                    $("#jijie").combobox("setValue", data.data.season);
                    $("#anquanjibie").combobox("setValue", data.data.anquanjibie);
                    $("#zhixingbiaozhun").combobox("setValue", data.data.zhixingbiaozhun);
                    $("#liliao").textbox("setValue", data.data.liliao);
                    $("#mianliao").textbox("setValue", data.data.mianliao);
                    $("#zhuangliao").textbox("setValue", data.data.zhuangliao);
                    $("#tianchongwu").textbox("setValue", data.data.tianchongwu);
                    $("#danbu").textbox("setValue", data.data.danbu);
                    $("#beizhu").textbox("setValue", data.data.beizhu);
                    if (data.data.state == 1) {
                        $("#kuanhao").textbox('textbox').attr('readonly', true);
                        $("#tishi").text("*该款号已被引用，故款号、颜色和尺码不可修改只可添加");
                    }
                    var colors = data.data.colors;
                    colors = colors.substring(1, colors.length - 1);
                    var color = colors.split(",");
                    var sumcolor = color.length - 3;
                    //看有多少个颜色超过3个就加input框
                    if (sumcolor > 0) {
                        var msg = '';
                        for (i = 0; i < sumcolor; i++) {
                            msg += '<tr><td><input  class="easyui-textbox color" style="width: 80px"></td></tr>'
                        }
                        var result = $("#yansezu").append(msg);
                        $.parser.parse(result);
                    }
                    //把所有颜色放进去
                    for (i = 0; i < color.length; i++) {
                        $(".color").eq(i).textbox("setValue", color[i]);
                        //禁止输入
                        if (data.data.state == 1) {
                            $(".color").eq(i).textbox('textbox').attr('readonly', true);
                        }
                    }
                    //尺码
                    var sizes = data.data.sizes;
                    sizes = sizes.substring(1, sizes.length - 1);
                    var size = sizes.split(",");
                    var sumsize = size.length - 5;
                    //看有多少个颜色超过3个就加input框
                    if (sumsize > 0) {
                        var msgs = '';
                        for (i = 0; i < sumsize; i++) {
                            msgs += '<tr><td><input  class="easyui-textbox size" style="width: 80px"></td></tr>'
                        }
                        var results = $("#chimazu").append(msgs);
                        $.parser.parse(results);
                    }
                    //把所有尺码放进去
                    for (i = 0; i < size.length; i++) {
                        $(".size").eq(i).textbox("setValue", size[i]);
                        //禁止输入
                        if (data.data.state == 1) {
                            $(".size").eq(i).textbox('textbox').attr('readonly', true);
                        }
                    }
                    $("#addwindow").window('open');
                }
            })
        } else {
            $.messager.alert('提示', '请选择要操作的数据');
            return;
        }
    } else {
        var date = new Date;
        var year = date.getFullYear();
        $('#year').combobox('setValue', year.toString());
        $("#id").val("")
        $('#addwindow').dialog({title: "新增款号"});
        $("#addwindow").window('open');
        loadCombobox();
    }
}
//保存款号信息
function save() {
    var kuanhao = $("#kuanhao").val().trim();
    if (kuanhao == "" || kuanhao == null) {
        $("#kuanhaotest").text('*款号不能为空');
        return;
    } else {
        $("#kuanhaotest").text('');
    }
    var kuanming = $("#kuanming").val();
    if (kuanming == "" || kuanming == null) {
        $("#kuanmingtest").text("*款名不能为空")
        return;
    } else {
        $("#kuanmingtest").text("")
    }
    var mingpaijia = $("#mingpaijia").val();
    if (mingpaijia == "" || mingpaijia == null) {
        $("#mingpaijiatest").text("铭牌价不能为空");
        return;
    } else if (mingpaijia < 0) {
        $("#mingpaijiatest").text("*铭牌价不能小于0");
        return;
    } else {
        $("#mingpaijiatest").text("")
    }
    var kuanshi = $("#kuanshi").combobox("getValue").trim();
    if (kuanshi == "" || kuanshi == null) {
        $("#kuanshitest").text("*款式不能为空");
        return;
    } else {
        $("#kuanshitest").text("");
    }
    var pinpai = $("#pinpai").combobox("getValue");
    if (pinpai == "" || pinpai == null) {
        $("#pinpaitest").text("*品牌不能为空");
        return;
    } else {
        $("#pinpaitest").text("");
    }
    var style = $("#kuanxing").combobox("getValue");
    if (style == "" || style == null) {
        $("#kuanxingtest").text("*款型不能为空");
        return;
    } else {
        $("#kuanxingtest").text("");
    }

    var dalei = $("#dalei").combobox("getValue");
    if (dalei == "" || dalei == null) {
        $("#daleitest").text("*大类不能为空");
        return;
    } else {
        $("#daleitest").text("");
    }

    var xiaolei = $("#xiaolei").combobox("getValue");
    if (xiaolei == "" || xiaolei == null) {
        $("#xiaoleitest").text("*小类不能为空");
        return;
    } else {
        $("#xiaoleitest").text("");
    }
    var gongyingshang = $("#gongyings").combobox("getValue");
    if (gongyingshang == "" || gongyingshang == null) {
        $("#gongyingstest").text("*供应商不能为空");
        return;
    } else {
        $("#gongyingstest").text("");
    }
    var xingbie = $("#xingbie").combobox("getValue");
    if (xingbie == "" || xingbie == null) {
        $("#xingbietest").text("*性别不能为空");
        return;
    } else {
        $("#xingbietest").text("");
    }
    var year = $("#year").combobox("getValue");

    var season = $("#jijie").combobox("getValue");
    if (season == "" || season == null) {
        $("#jijietest").text("*季节不能为空");
        return;
    } else {
        $("#jijietest").text("");
    }

    var anquanjibie = $("#anquanjibie").combobox("getValue");
    if (anquanjibie == "" || anquanjibie == null) {
        $("#anquanjibietest").text("*安全级别不能为空");
        return;
    } else {
        $("#anquanjibietest").text("");
    }
    var zhixingbiaozhun = $("#zhixingbiaozhun").combobox("getValue");
    if (zhixingbiaozhun == "" || zhixingbiaozhun == null) {
        $("#zhixingbiaozhuntest").text("*执行标准不能为空");
        return;
    } else {
        $("#zhixingbiaozhuntest").text("");
    }
    var liliao = $("#liliao").val();
    var mianliao = $("#mianliao").val();
    var zhuangliao = $("#zhuangliao").val();
    var tianchongwu = $("#tianchongwu").val();
    var danbu = $("#danbu").val();
    var beizhu = $("#beizhu").val();
    var colors = "";
    var sizes = "";
    //获取所有颜色
    for (i = 0; i < $(".color").length; i++) {
        var mes = $(".color").eq(i).val().trim();
        if (mes != "") {
            if (i == $(".color").length - 1) {
                colors += mes;
                break;
            }
            colors += mes + ','
        }
    }
    //获取所有尺码
    for (i = 0; i < $(".size").length; i++) {
        var mes = $(".size").eq(i).val().trim();
        if (mes != "") {
            if (i == $(".size").length - 1) {
                sizes += mes;
                break;
            }
            sizes += mes + ','
        }
    }

    if (colors.trim() == "") {
        $.messager.alert('警告', "至少填写一个颜色")
        return;
    }
    if (sizes.trim() == "") {
        $.messager.alert('警告', "至少填写一个尺码")
        return;
    }

    $.ajax({
        url: path + '/production/save.do',
        dataType: 'json',
        type: 'POST',
        data: {
            id: $("#id").val(),
            product: kuanhao,
            productName: kuanming,
            dalei: dalei,
            category: xiaolei,
            sex: xingbie,
            price: mingpaijia,
            kuanshi: kuanshi,
            gongyingshang: gongyingshang,
            year: year,
            season: season,
            anquanjibie: anquanjibie,
            zhixingbiaozhun: zhixingbiaozhun,
            colors: colors,
            sizes: sizes,
            liliao: liliao,
            mianliao: mianliao,
            zhuangliao: zhuangliao,
            tianchongwu: tianchongwu,
            pinpai: pinpai,
            style: style,
            danbu: danbu,
            beizhu: beizhu
        },
        success: function (data) {
            if (data.code == 200) {
                var sts = "";
                if ($("#id").val().trim() != "" && $("#id").val().trim() != null) {
                    sts = '修改';
                } else {
                    sts = '添加';
                }
                $.messager.show({
                    title: '提示',
                    msg: sts + kuanhao + '成功!',
                    timeout: 3000,
                    showType: 'slide'
                });
                $("#dg").datagrid({
                    url: path + '/production/findall.do'
                });
                $("#addwindow").window('close');
            } else {
                $.messager.alert('警告', data.msg);
            }
        }
    })
}
//添加颜色输入框
function addYanse() {
    var msg = '<tr><td><input  class="easyui-textbox color" style="width: 80px"></td></tr>'
    var result = $("#yansezu").append(msg);
    $.parser.parse(result);
}
//添加尺码输入框
function addChima() {
    var msg = '<tr><td><input  class="easyui-textbox size" style="width: 80px"></td></tr>'
    var result = $("#chimazu").append(msg);
    $.parser.parse(result);
}
//加载所有combobox
function loadCombobox() {
    $.ajax({
            url: path + '/production/combobox.do',
            dataType: 'json',
            type: 'POST',
            success: function (data) {
                if (data.code == 200) {
                    var data = data.data;

                    if (data.kuanshi != null && data.kuanshi != '') {
                        $('#kuanshi').combobox('loadData', data.kuanshi);
                    }
                    /* if (data.jijie != null && data.jijie != '') {
                     $('#jijie').combobox('loadData', data.jijie);
                     }*/
                    if (data.anquanjibie != null && data.anquanjibie != '') {
                        $('#anquanjibie').combobox('loadData', data.anquanjibie);
                    }
                    if (data.zhixingbiaozhun != null && data.zhixingbiaozhun != '') {
                        $('#zhixingbiaozhun').combobox('loadData', data.zhixingbiaozhun);
                    }
                    if (data.xiaolei != null && data.xiaolei != '') {
                        $('#xiaolei').combobox('loadData', data.xiaolei);
                    }
                    /*if (data.color != null && data.color != '') {
                     $('#color').combobox('loadData', data.color);
                     }
                     if (data.size != null && data.size != '') {
                     $('#size').combobox('loadData', data.size);
                     }*/
                    if (data.gongyingshang != null && data.gongyingshang != '') {
                        $('#gongyings').combobox('loadData', data.gongyingshang);
                    }
                    if (data.sex != null && data.sex != '') {
                        $('#xingbie').combobox('loadData', data.sex);
                    }
                    if (data.dalei != null && data.dalei != '') {
                        $('#dalei').combobox('loadData', data.dalei);
                    }
                    if (data.kuanxing != null && data.kuanxing != '') {
                        $('#kuanxing').combobox('loadData', data.kuanxing);
                    }

                    if (data.pinpai != null && data.pinpai != '') {
                        $('#pinpai').combobox('loadData', data.pinpai);
                    }
                } else {
                    $.messager.alert('警告', data.msg);
                }

            }
        }
    )
}
//清除所有值
function clear() {
    $("#id").val("")
    $("#kuanhao").textbox("setValue", "");
    $("#kuanming").textbox("setValue", "");
    $("#mingpaijia").numberbox("setValue", "");
    $("#kuanshi").combobox("setValue", "");
    $("#pinpai").combobox("setValue", "");
    $("#kuanxing").combobox("setValue", "");
    $("#dalei").combobox("setValue", "");
    $("#xiaolei").combobox("setValue", "");
    $("#gongyings").combobox("setValue", "");
    $("#xingbie").combobox("setValue", "");
    $("#year").combobox("setValue", "");
    //$("#jijie").combobox("setValue", "");
    $("#anquanjibie").combobox("setValue", "");
    $("#zhixingbiaozhun").combobox("setValue", "");
    $("#liliao").textbox("setValue", "");
    $("#mianliao").textbox("setValue", "");
    $("#zhuangliao").textbox("setValue", "");
    $("#tianchongwu").textbox("setValue", "");
    $("#danbu").textbox("setValue", "");
    $("#beizhu").textbox("setValue", "");
    $(".color").textbox("setValue", "");
    $(".size").textbox("setValue", "");
    $("#yansezu").html("");
    $("#chimazu").html("");
    $("#kuanhaotest").text('');
    $("#kuanmingtest").text("")
    $("#mingpaijiatest").text("")
    $("#kuanshitest").text("");
    $("#pinpaitest").text("");
    $("#kuanxingtest").text("");
    $("#daleitest").text("");
    $("#xiaoleitest").text("");
    $("#gongyingstest").text("");
    $("#xingbietest").text("");
    $("#jijietest").text("");
    $("#anquanjibietest").text("");
    $("#zhixingbiaozhuntest").text("");
    for (i = 0; i < 5; i++) {
        $(".size").eq(i).textbox('textbox').attr('readonly', false);
    }
    for (i = 0; i < 3; i++) {
        $(".color").eq(i).textbox('textbox').attr('readonly', false);
    }
    $("#kuanhao").textbox('textbox').attr('readonly', false);
    $("#tishi").text("");
}

//打开下级新增入口
function openDownWindow(code) {
    clearDownWindow();
    //新增
    if (code == 0) {
        var date = new Date;
        var year = date.getFullYear();
        $('#downyear').combobox('setValue', year.toString());
        $("#downid").val("")
        $('#downwindow').dialog({title: "新增款号"});
        $('#downwindow').window('open')
    } else {
        $('#downwindow').dialog({title: "修改款号"});
        var row = $('#dg').datagrid('getSelected');
        if (row) {
            if (row.state != 0) {
                $.messager.alert('提示', '该状态下不可修改信息');
                return;
            }
            $.ajax({
                url: path + '/production/find/id.do',
                dataType: 'json',
                type: 'POST',
                data: {
                    id: row.id
                },
                success: function (data) {
                    $("#downid").val(data.data.id);
                    $("#shangpinbianhao").textbox("setValue", data.data.product);
                    $("#downkuanming").textbox("setValue", data.data.productName);
                    $("#downmingpaijia").numberbox("setValue", data.data.price);
                    $("#downxingbie").combobox("setValue", data.data.sex);
                    $("#downyear").combobox("setValue", data.data.year);
                    $("#downjijie").combobox("setValue", data.data.season);
                    $("#downcolor").textbox("setValue", data.data.colors.substring(1, data.data.colors.length - 1));
                    $("#downsize").textbox("setValue", data.data.sizes.substring(1, data.data.sizes.length - 1));
                    if (data.data.state == 1) {
                        $("#shangpinbianhao").textbox('textbox').attr('readonly', true);
                        $("#downcolor").textbox('textbox').attr('readonly', true);
                        $("#downsize").textbox('textbox').attr('readonly', true);
                    } else {
                        $("#shangpinbianhao").textbox('textbox').attr('readonly', false);
                        $("#downcolor").textbox('textbox').attr('readonly', false);
                        $("#downsize").textbox('textbox').attr('readonly', false);
                    }
                    $("#downwindow").window('open');
                }
            })
        } else {
            $.messager.alert('提示', '请选择要操作的数据');
            return;
        }
    }

}
//保存款号
function saveDown() {
    var product = $("#shangpinbianhao").textbox("getValue");
    if (product == null || product.trim() == "") {
        $("#shangpinbianhaotest").text("*商品编号不能为空");
        return;
    } else {
        $("#shangpinbianhaotest").text("");
    }
    var productName = $("#downkuanming").textbox("getValue");
    if (productName == null || productName.trim() == "") {
        $("#downkuanmingtest").text("*款名不能为空");
        return;
    } else {
        $("#downkuanmingtest").text("");
    }
    var price = $("#downmingpaijia").numberbox("getValue");
    if (price == null || price.trim() == "" || price == 0) {
        $("#downmingpaijiatest").text("*价格不能为空");
        return;
    } else {
        $("#downmingpaijiatest").text("");
    }
    var year = $("#downyear").combobox("getValue");
    var season = $("#downjijie").combobox("getValue");
    var sex = $("#downxingbie").combobox("getValue");
    var color = $("#downcolor").textbox("getValue");
    if (color == null || color.trim() == "") {
        $("#downcolortest").text("*颜色不能为空");
        return;
    } else {
        $("#downcolortest").text("");
    }
    var size = $("#downsize").textbox("getValue");
    if (size == null || size.trim() == "") {
        $("#downsizetest").text("*尺码不能为空");
        return;
    } else {
        $("#downsizetest").text("");
    }
    $.ajax({
        url: path + '/production/savedown.do',
        dataType: 'json',
        data: {
            id: $("#downid").val(),
            product: product,
            productName: productName,
            price: price,
            sex: sex,
            year: year,
            season: season,
            colors: color,
            sizes: size
        },
        type: 'POST',
        success: function (data) {
            if (data.code == 200) {
                var sts = '';
                if ($("#downid").val().trim() != "" && $("#downid").val().trim() != null) {
                    sts = '修改';
                } else {
                    sts = '添加';
                }
                $.messager.show({
                    title: '提示',
                    msg: sts + product + '成功!',
                    timeout: 3000,
                    showType: 'slide'
                });
                $("#dg").datagrid({
                    url: path + '/production/findall.do'
                });
                $("#downwindow").window('close');
            } else {
                $.messager.alert('警告', data.msg);
            }
        }
    })
}

function clearDownWindow() {
    $("#shangpinbianhao").textbox("setValue", "");
    $("#shangpinbianhaotest").text("");
    $("#downkuanming").textbox("setValue", "");
    $("#downkuanmingtest").text("");
    $("#downmingpaijia").numberbox("setValue", "");
    $("#downmingpaijiatest").text("");
    $("#downcolor").textbox("setValue", "");
    $("#downcolortest").text("");
    $("#downsize").textbox("setValue", "");
    $("#downsizetest").text("");
    $("#downjijie").combobox("setValue", "春季");
    $("#downjijietest").text("");
    $("#downxingbie").combobox("setValue", "男");
    $("#downxingbietest").text("");
    $("#downid").val("");
}